package com.asset.dispoal.dicData.service.commom;

import com.asset.dispoal.commom.page.PagingQuery;
import com.asset.dispoal.dicData.domain.db.entity.GbDictData;
import com.asset.dispoal.dicData.domain.db.mapper.GbDictDataMapper;
import com.asset.dispoal.dicData.dto.GbDictDataRequest;
import com.asset.dispoal.dicData.service.converter.GbDictDataConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/2/24$
 */
@Component
public class QueryPageCommand implements BaseCommand<GbDictDataRequest, Result> {

    @Autowired
    private GbDictDataMapper gbDictDataMapper;

    @Override
    public Result<PageVO> execute(GbDictDataRequest gbDictDataRequest) {
        GbDictData gbDictData = GbDictDataConverter.INSTANCE.reqToDo(gbDictDataRequest);
        int count = gbDictDataMapper.pageCount(gbDictData);
        Page<GbDictData> page = PagingQuery.getPage(count, gbDictData);
        PageVO<GbDictData> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            final List<GbDictData> dataList = gbDictDataMapper.page(page);
            pageVO.setResult(dataList);
        }
        return Result.ok(pageVO);
    }
}
